<?php namespace Phpcmf\Controllers;

class Post extends \Phpcmf\Member\Module {


    public function index() {

        $this->_module_init(APP_DIR);

		if (IS_AJAX_POST) {
		    $catid = intval(\Phpcmf\Service::L('Input')->post('catid'));
		    if (!$catid) {
		        $this->_msg(0, '请选择分类', '');
		    }
		}
		
		$select = '';
		
		
		/*$category = $this->_get_module_member_category($this->module, 'add');
		dr_debug('category', $category);*/
		/*if (!$category[$catid]) {
		    $this->_msg(0, dr_lang('当前板块(%s)没有发布权限', (int)$catid));
		}*/
		
		if (!$this->is_hcategory) {
		    // 走栏目权限
		    $category = $this->_get_module_member_category($this->module, 'add');
		    // 栏目id不存在时就去第一个可用栏目为catid
		    if (!$catid) {
		        list($select, $catid) = \Phpcmf\Service::L('Tree')->select_category(
		            $category,
		            $catid,
		            'id=\'dr_catid\' name=\'catid\' onChange="show_category_field(this.value)"',
		            '', 1, 1, 1
		        );
		    } else {
		        $select = \Phpcmf\Service::L('Tree')->select_category(
		            $category,
		            $catid,
		            'id=\'dr_catid\' name=\'catid\' onChange="show_category_field(this.value)"',
		            '', 1, 1
		        );
		    }
			if (!$catid) {
			    if ($this->uid) {
		            $this->_msg(0, dr_lang('没有可用的栏目发布权限'));
		        } else {
		            $this->_msg(0, dr_lang('您没有登录'), dr_member_url('login/index'));
		        }
		    } elseif (!$category[$catid]) {
		        $this->_msg(0, dr_lang('当前栏目(%s)没有发布权限', $this->module['category'][$catid]['name']));
		    }
		    $this->is_post_code = \Phpcmf\Service::M('member_auth')->category_auth($this->module, $catid, 'code', $this->member);
		} else {
		    // 不走栏目权限，走自定义权限
		    $rt = $this->content_model->_hcategory_member_add_auth();
		    if (!$rt['code']) {
		        $this->_msg(0, $rt['msg'], $rt['data']);
		    }
		    $this->is_post_code = $this->content_model->_hcategory_member_post_code();
		}
		
		

        $this->is_get_catid = $catid;

        $this->_Post();

        \Phpcmf\Service::V()->assign([
            'cat' => $category[$catid],
            'form' => dr_form_hidden(['catid' => $catid, 'is_draft' => 0, 'module' => $this->module['dirname'], 'id' => 0]),
            'catid' => $catid,
            'module' => $this->module,
			'select' => $select,
            'post_url' => dr_now_url(),
            'reply_url' => $category[$catid]['url'],
            'meta_title' => '反馈'.SITE_SEOJOIN.$this->module['site'][SITE_ID]['module_title'],
        ]);

        \Phpcmf\Service::V()->display('post.html');
    }
	protected function _Call_Post($data) {
	
	    if ($data[1]['status'] == 9) {
	        $list = $html = '';
	        if ($this->module['category'][$data[1]['catid']]['setting']['html']) {
	            // 生成权限文件
	            if (!dr_html_auth(1)) {
	                return dr_return_data(0, dr_lang('/cache/html/ 无法写入文件'));
	            }
	            $html = WEB_DIR.'index.php?s='.$this->module['dirname'].'&c=html&m=showfile&id='.$data[1]['id'];
	            $list = WEB_DIR.'index.php?s='.$this->module['dirname'].'&c=html&m=categoryfile&id='.$data[1]['catid'];
	        }
	            $url = WEB_DIR.'index.php?s='.$this->module['dirname'].'&c=show&id='.$data[1]['id'];
	        return dr_return_data(1, dr_lang('操作成功'), ['url' => $url, 'id' => $data[1]['id'], 'catid' => $data[1]['catid'], 'htmlfile' => $html, 'htmllist' => $list]);
	    } else {
	        if (\Phpcmf\Service::L('input')->post('is_draft')) {
	            return dr_return_data(1, dr_lang('操作成功，已存储到草稿箱'));
	        } else {
	            return dr_return_data(1, dr_lang('操作成功，等待管理员审核'), ['id' => $data[1]['id'], 'catid' => $data[1]['catid']]);
	        }
	    }
	}

}
